import 'package:flutter/material.dart';
import 'package:flutter_common/config/config.dart';
import 'package:flutter_common/widget/loading.dart';
import 'package:flutter_common/net/http.dart';
import 'package:flutter_fun_app/model/userModel.dart';
import 'package:flutter_common/widget/toast.dart';
import 'package:flutter_fun_app/utils/setting.dart';
import 'dart:ui';
import 'package:fluintl/fluintl.dart';
import 'package:flutter_fun_app/utils/strings.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return LoginState();
  }
}

class LoginState extends State<LoginPage> {
  TextEditingController phoneController = TextEditingController();
  TextEditingController pwdController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    phoneController.text = '13594347817';
    pwdController.text = '123456';
  }

  Future<void> onLogin() async {
    Loading.show(context);
    var data = {
      'key': '00d91e8e0cca2b76f515926a36db68f5',
      'phone': phoneController.text,
      'passwd': pwdController.text,
    };
    HttpUtil.get(Api.login, data: data, success: (data) {
      UserModel userModel = UserModel.fromJson(data);
      Setting.setLogin(true);
      Setting.saveUserInfo(userModel.data);
      Navigator.pop(context, true);
    }, error: (err) {
      ToastUtil.show(err);
    }, then: () {
      Navigator.pop(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        body: Stack(
      children: <Widget>[
        Image.asset("images/login2.jpg",
            fit: BoxFit.fill, width: double.infinity, height: double.infinity),
        BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 3, sigmaY: 3),
          child: new Container(
              color: Colors.white.withOpacity(0.1),
              width: double.infinity,
              height: double.infinity),
        ),
        Container(
          padding: EdgeInsets.all(30),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                controller: phoneController,
                maxLines: 1,
                maxLength: 11,
                cursorColor: Colors.grey,
                style: TextStyle(color: Config.titleBarColor),
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: IntlUtil.getString(context, Ids.login_hint_account),
                ),
              ),
              TextField(
                  controller: pwdController,
                  maxLines: 1,
                  obscureText: true,
                  style: TextStyle(color: Config.titleBarColor),
                  cursorColor: Colors.grey,
                  decoration: InputDecoration(
                    hintText: IntlUtil.getString(context, Ids.login_hint_pwd),
                  )),
              Container(
                margin: EdgeInsets.all(15),
                child: MaterialButton(
                  onPressed: () {
                    onLogin();
                  },
                  color: Config.titleBarColor,
                  child: Text(
                    IntlUtil.getString(context, Ids.login_confirm),
                    style: TextStyle(color: Config.textWhite),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    ));
  }
}
